package com.lbs.droiddoctor.disease;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbs.droiddoctor.R;
import com.lbs.droiddoctor.base.BaseActivity;
import com.lbs.droiddoctor.common.HttpASync;
import com.lbs.droiddoctor.common.HttpASync.OnRequest;
import com.lbs.droiddoctor.constants.AppConstants;
import com.lbs.view.adapter.DiseaseAdapter;
import com.lbs.view.adapter.PredictDiseaseAdapter;
import com.lbs.view.adapter.SymptomsAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
//this pgm predicts disease
public class SearchDisease extends BaseActivity implements OnClickListener,
		OnRequest {

	private Button ok;
	private EditText symptom;
	private ListView symptomsList;
	private ListView predictList;
	private SymptomsAdapter symptomsAdapter;
	private SymptomsAdapter predictSymptomsAdapter;
	private ArrayList<String> symptoms;
	private String predictUrl = AppConstants.PREDICT_PATH + "?";
	private int symReq;
	private DiseaseAdapter adapter;
	private Context ctx;
	private ArrayList<String> predicted = new ArrayList<String>();
	private ArrayList<Symptom> predict;
	private ArrayList<Disease> diseaseListData;
	private ListView diseaseList;
	private ArrayList<Disease> predictedDiseases;
	private PredictDiseaseAdapter diseaseAdapter;
private final int LEVEL=1;
	private Handler uiHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {

				int position = msg.arg1;

				symptoms.remove(position);

				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						symptomsAdapter.notifyDataSetChanged();

					}
				});

			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchdisease);
		ctx = this;
		ok = (Button) findViewById(R.id.submit);
		symptom = (EditText) findViewById(R.id.editText1);
		symptomsList = (ListView) findViewById(R.id.listsymptoms);
		predictList = (ListView) findViewById(R.id.list_predicted_symptoms);
		ok.setOnClickListener(this);
		System.out.println("diss");
		diseaseList = (ListView) findViewById(R.id.listView1);

		symReq = 1;

		symptoms = new ArrayList<String>();
		symptomsAdapter = new SymptomsAdapter(this, symptoms, uiHandler);
		symptomsList.setAdapter(symptomsAdapter);

		predictList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {

				String selectedSymptom = predicted.get(position);
				symptom.setText(selectedSymptom);
			}
		});
		// predictedDiseases = new ArrayList<Disease>();

		diseaseList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {

			}
		});

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.submit) {
			String sname = symptom.getText().toString();
			try {
				sname = URLEncoder.encode(sname, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (sname.equals("")) {
				Toast.makeText(this, "Please enter symptom name",
						Toast.LENGTH_SHORT).show();
			} else {

				symptoms.add(sname);
				symptomsAdapter.notifyDataSetChanged();

				symReq++;
				if (symptoms.size() <= LEVEL) {
					predictUrl = AppConstants.PREDICT_PATH + "?";
					if (symptoms.size() == 1) {
						predictUrl += "sym1=" + sname;
					} else if (symptoms.size() <= LEVEL) {
						for (int i = 0; i < symptoms.size(); i++) {
							if (i == 0) {
								predictUrl += "sym1=" + symptoms.get(i);
							} else {
								predictUrl += "&sym" + i + "="
										+ symptoms.get(i);
							}

						}
						// predictUrl += "&sym" + symReq + "=" + sname;
					}
					HttpASync request = new HttpASync();
					request.setOnRequestListener(this);

					request.execute(predictUrl);
					showProgress("Predicting symptoms...");

				} else {
					String predictDiseasePath = AppConstants.PREDICT_DISEASE_PATH
							+ "?sym=" + symptoms.get(0);
					for (int i = 1; i < symptoms.size(); i++) {
						predictDiseasePath += "," + symptoms.get(i);
					}
					HttpASync request = new HttpASync();
					request.setOnRequestListener(this);

					request.execute(predictDiseasePath);
					showProgress("Predicting Diseases...");

				}

			}

		}
	}

	@Override
	public void onResponseReceived(String response) {
		Gson gson = new Gson();
		if (symptoms.size() <= LEVEL) {
			predict = gson.fromJson(response, new TypeToken<List<Symptom>>() {
			}.getType());

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub

					hideProgress();

					predicted = new ArrayList<String>();
					for (Symptom sym : predict) {
						System.out.println("Adding : " + sym.getSymptomName());

						predicted.add(sym.getSymptomName());

					}

					predictSymptomsAdapter = new SymptomsAdapter(ctx,
							predicted, uiHandler);
					predictSymptomsAdapter.setChangeIcon(true);
					predictList.setAdapter(predictSymptomsAdapter);
				}

			});
		} else {
			System.out.println("res:" + response);
			predictedDiseases = gson.fromJson(response,
					new TypeToken<List<Disease>>() {
					}.getType());
			final String resStr = response;
			runOnUiThread(new Runnable() {

				@Override
				public void run() {

					hideProgress();
					if (predictedDiseases.size() <= 0) {
						// Toast.makeText(ctx, "No disease found... ",
						// Toast.LENGTH_LONG).show();
						predictedDiseases = new ArrayList<Disease>();
						Disease dis = new Disease();
						dis.setDiseaseName("No Disease found");
						predictedDiseases.add(dis);
						diseaseAdapter = new PredictDiseaseAdapter(ctx,
								predictedDiseases);
						diseaseList.setAdapter(diseaseAdapter);
					} else {
						diseaseAdapter = new PredictDiseaseAdapter(ctx,
								predictedDiseases);
						diseaseList.setAdapter(diseaseAdapter);
						
						diseaseList.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
									View v, int position, long id) {
							
								String disease = predictedDiseases.get(position).getDiseaseName();
								Intent i = new Intent(ctx,DiseaseSymptoms.class);
								i.putExtra("DISEASE", disease);
								ctx.startActivity(i);
								
							}
						});
					}
				}
			});

		}
	}

}
